//
//  XJLocalTravelDetailVC.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/3.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJLocalTravelDetailVC.h"
#import "XJYMTTeiZiDetailViewController.h" // 复用 详情

//view
#import "XJYMTTableViewCell.h" //复用
//model
#import "XJYMTModel.h" //复用


@interface XJLocalTravelDetailVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation XJLocalTravelDetailVC


#pragma mark - 一 lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        
        NSString *cellId = NSStringFromClass([XJYMTTableViewCell class]);
        
        [_tableView registerClass:[XJYMTTableViewCell class] forCellReuseIdentifier:cellId];
        _tableView.tableFooterView = [UIView new];

    }
    return _tableView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


#pragma mark - 二 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView.frame = self.view.bounds;
    [self.view addSubview:self.tableView];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self requestInvitationCtyListDataWithCity:@"杭州"];
}

#pragma mark - 三 -1 UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *cellId = NSStringFromClass([XJYMTTableViewCell class]);

    XJYMTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    XJYMTModel *model = self.dataArray[indexPath.row];
    
    [cell configureYMTCellWithAvator:model.avatar time:model.publish_time name:model.publish_name title:model.title content:model.content clickCount:model.click_count];
    
    return cell;
}


#pragma mark - 三 -2 UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XJLog(@"%ld---%ld",indexPath.section,indexPath.row);
    
    XJYMTTeiZiDetailViewController *tieziDetailVC = [[XJYMTTeiZiDetailViewController alloc] init];
    
    XJYMTModel *model = self.dataArray[indexPath.row];
    tieziDetailVC.navigationItem.title = model.title;
    
    tieziDetailVC.tieZiId = model.tieZiId;
    tieziDetailVC.type = @"city";
    [self.navigationController pushViewController:tieziDetailVC animated:YES];
    
}


#pragma mark - 四 网络请求
/**请求具体的帖子对应分类的详细数据*/
- (void)requestInvitationCtyListDataWithCity:(NSString *)cityStr {
    
    
    if ([NSString isEmptyString:self.cityCategoryId]) { //如果为0 就请求全部数据
        self.cityCategoryId = @"0";
    }
    
    
    NSNumber *page = @(1);
    NSNumber *page_size = @(10);
    NSString *invitation_type = @"city";
    NSString *city = cityStr;
    NSString *category_id = self.cityCategoryId; //默认为0,表示全部
    NSDictionary *parDic = NSDictionaryOfVariableBindings(page,page_size,invitation_type,city,category_id);
    XJLog(@"请求参数:%@",parDic);
    
    [XJHttpTool requestInvitationListWithDic:parDic succesH:^(id responseObjc) {
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            NSArray *list = responseObjc[@"data"][@"list"];
            
            for (NSDictionary *tempDic in list) {
                XJYMTModel *model = [XJYMTModel mj_objectWithKeyValues:tempDic];
                
                [self.dataArray addObject:model];
            }
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } failH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
